package org.pr.kafkaexpress.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.admin.NewTopic;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.serialization.StringSerializer;
import org.pr.kafkaexpress.constants.KafkaConstants;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.config.TopicBuilder;
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.core.ProducerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * Kafka配置类
 * 
 * @author LXC
 * @since 2025/7/10
 */
@Slf4j
@Configuration
public class KafkaConfig {

    @Value("${spring.kafka.bootstrap-servers:localhost:9092}")
    private String bootstrapServers;

    @Value("${spring.kafka.producer.acks:all}")
    private String acks;

    @Value("${spring.kafka.producer.retries:3}")
    private int retries;

    @Value("${spring.kafka.producer.batch-size:16384}")
    private int batchSize;

    @Value("${spring.kafka.producer.linger-ms:1}")
    private int lingerMs;

    @Value("${spring.kafka.producer.buffer-memory:33554432}")
    private int bufferMemory;

    /**
     * 生产者工厂配置
     */
    @Bean
    public ProducerFactory<String, String> producerFactory() {
        Map<String, Object> configProps = new HashMap<>();
        configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
        configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        configProps.put(ProducerConfig.ACKS_CONFIG, acks);
        configProps.put(ProducerConfig.RETRIES_CONFIG, retries);
        configProps.put(ProducerConfig.BATCH_SIZE_CONFIG, batchSize);
        configProps.put(ProducerConfig.LINGER_MS_CONFIG, lingerMs);
        configProps.put(ProducerConfig.BUFFER_MEMORY_CONFIG, bufferMemory);
        
        log.info("Kafka producer factory configured with bootstrap servers: {}", bootstrapServers);
        return new DefaultKafkaProducerFactory<>(configProps);
    }

    /**
     * Kafka模板配置
     */
    @Bean
    public KafkaTemplate<String, String> kafkaTemplate() {
        return new KafkaTemplate<>(producerFactory());
    }

    /**
     * 创建测试主题
     */
    @Bean
    public NewTopic testTopic() {
        log.info("Creating test topic: {}", KafkaConstants.TEST_TOPIC);
        return TopicBuilder.name(KafkaConstants.TEST_TOPIC)
                .partitions(KafkaConstants.DEFAULT_PARTITIONS)
                .replicas(KafkaConstants.REPLICATION_FACTOR)
                .build();
    }

    /**
     * 创建批量处理主题
     */
    @Bean
    public NewTopic batchTopic() {
        log.info("Creating batch topic: {}", KafkaConstants.BATCH_TOPIC);
        return TopicBuilder.name(KafkaConstants.BATCH_TOPIC)
                .partitions(KafkaConstants.DEFAULT_PARTITIONS)
                .replicas(KafkaConstants.REPLICATION_FACTOR)
                .build();
    }

    /**
     * 创建死信队列主题
     */
    @Bean
    public NewTopic deadLetterTopic() {
        log.info("Creating dead letter topic: {}", KafkaConstants.DEAD_LETTER_TOPIC);
        return TopicBuilder.name(KafkaConstants.DEAD_LETTER_TOPIC)
                .partitions(KafkaConstants.DEFAULT_PARTITIONS)
                .replicas(KafkaConstants.REPLICATION_FACTOR)
                .build();
    }
} 